<%@ page import="org.slf4j.Logger" %>
<%@ page import="org.slf4j.LoggerFactory" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
  Logger log = LoggerFactory.getLogger("edu.polsl.doDownload.jsp");
  FileInputStream fIn = null;
  try {
    String filename = request.getParameter("filename");
    log.debug("Downloading {}", filename);

    ServletOutputStream op = response.getOutputStream();

    File file = new File(filename);

    response.setContentType("application/octet-stream");
//        resp.setContentLength( (int)f.length() );
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

    fIn = new FileInputStream(file);
    byte[] buf = new byte[100000];
    int size;
    while ((size = fIn.read(buf)) != -1) {
      op.write(buf, 0, size);
    }


    op.flush();
    op.close();

  } catch (Exception e) {
    log.error("Exception occurred", e);
    throw e;
  } finally {
    if (fIn != null) {
      try {
        fIn.close();
      } catch (IOException e) {
        log.error("Exception occurred during closing stream", e);
      }
    }
  }
%>